﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Designers
{
    public class UiControlDesigner : DocumentDesigner, ILayoutFrameDesignerEvents
    {
        public event EventHandler<FrameDesignerEventArgs> InitializedNewComponent;

        /// <summary>
        /// Prepares the designer to view, edit, and design the specified component.
        /// </summary>
        /// <param name="component">The component for this designer.</param>
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            // Only UiControl supported
            if (component.GetType() != typeof(UiControl))
                throw new NotSupportedException("component");
        }

        /// <summary>
        /// Provides core functionality for all the <see cref="M:System.Windows.Forms.Design.ParentControlDesigner.CreateTool(System.Drawing.Design.ToolboxItem)"/> methods.
        /// </summary>
        /// <param name="tool">The <see cref="T:System.Drawing.Design.ToolboxItem"/> to create a component from.</param>
        /// <param name="x">The horizontal position, in design-time view coordinates, of the location of the left edge of the tool, if a size is specified; the horizontal position of the center of the tool, if no size is specified.</param>
        /// <param name="y">The vertical position, in design-time view coordinates, of the location of the top edge of the tool, if a size is specified; the vertical position of the center of the tool, if no size is specified.</param>
        /// <param name="width">The width of the tool. This parameter is ignored if the <paramref name="hasSize"/> parameter is set to false.</param>
        /// <param name="height">The height of the tool. This parameter is ignored if the <paramref name="hasSize"/> parameter is set to false.</param>
        /// <param name="hasLocation">true if a location for the component is specified; false if the component is to be positioned in the center of the currently selected control.</param>
        /// <param name="hasSize">true if a size for the component is specified; false if the default height and width values for the component are to be used.</param>
        /// <returns>
        /// An array of components created from the tool.
        /// </returns>
        protected override IComponent[] CreateToolCore(ToolboxItem tool, int x, int y, int width, int height, bool hasLocation, bool hasSize)
        {
            // Check whether root designer is in virtual state. 
            var isInVirtualState = ((UiControl)this.Control).IsInVirtualState;

            if (isInVirtualState)
                return new IComponent[0];

            return base.CreateToolCore(tool, x, y, width, height, hasLocation, hasSize);
        }
    }
}
